package cn.star.jots.dictoption.controller;

import cn.star.jots.base.response.JsonResult;
import cn.star.jots.dictoption.pojo.dto.DictOptionQuery;
import cn.star.jots.dictoption.pojo.dto.DictOptionSaveParam;
import cn.star.jots.dictoption.pojo.vo.DictOptionVO;
import cn.star.jots.dictoption.service.DictOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/dictOption")
@Api(tags = "字典选项管理")
@Slf4j
public class DictOptionController {
    @Autowired
    DictOptionService dictOptionService;

    @GetMapping("/select")
    @ApiOperation(value = "查询字典选项列表")
    public JsonResult select(DictOptionQuery dictOptionQuery) {
        log.info("查询字典选项列表:dictOptionQuery={}", dictOptionQuery);
        List<DictOptionVO> list = dictOptionService.selectAll(dictOptionQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存字典选项")
    public JsonResult save(DictOptionSaveParam dictOptionSaveParam) {
        log.info("保存字典选项:dictOptionSaveParam={}", dictOptionSaveParam);
        dictOptionService.save(dictOptionSaveParam);
        return JsonResult.ok();
    }
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除字典选项")
    public JsonResult delete(@PathVariable Long id) {
        log.info("删除字典选项:id={}", id);
        dictOptionService.delete(id);
        return JsonResult.ok();
    }
    // 定义一个根据字典code查其对应的所有字典项的方法
    @GetMapping("/select/{code}")
    @ApiOperation(value = "根据字典code查询字典项")
    public JsonResult selectDictOptionByCode(@PathVariable String code) {
        log.info("根据字典code查询字典项:code={}", code);
        List<DictOptionVO> list = dictOptionService.selectDictOptionByCode(code);
        return JsonResult.ok(list);
    }

}
